cmake_minimum_required(VERSION 3.14)
project (GraphEngine[CXX])

set(GE_CODE_DIR ${CMAKE_CURRENT_LIST_DIR})
set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY TRUE)

if (NOT BUILD_PATH)
    set(BUILD_PATH "${CMAKE_SOURCE_DIR}/build")
endif()

option(ENABLE_OPEN_SRC "Enable graphengine compile in opensource." FALSE)

if (ENABLE_OPEN_SRC)
    set(HI_PYTHON python3.7)

    include(cmake/external_libs/protobuf_shared.cmake)
    include(cmake/external_libs/protobuf_static.cmake)
    include(cmake/external_libs/protoc.cmake)
    include(cmake/external_libs/gflags.cmake)
    include(cmake/external_libs/securec.cmake)
    include(cmake/external_libs/json.cmake)
    include(cmake/FindModule.cmake)
    include(cmake/intf_pub_linux.cmake)

    # for CPU/GPU mode, find c_sec and slog from local prebuild
    #if(NOT ENABLE_D AND NOT GE_ONLY)
    #    set(GE_PREBUILD_PATH ${GE_CODE_DIR}/third_party/prebuild/${CMAKE_HOST_SYSTEM_PROCESSOR})
    #    find_module(slog libslog.so ${GE_PREBUILD_PATH})
    # if D_LINK_PATH is set in environment variables, search libraries in given path
    if(DEFINED ENV{D_LINK_PATH})
        # D_LINK_PATH is set
        set(GE_LIB_PATH $ENV{D_LINK_PATH})
        set(GE_SYS_ARCH "")
        if(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "x86_64")
            # x86 ubuntu
            set(GE_SYS_ARCH "x86_64")
        elseif(CMAKE_HOST_SYSTEM_PROCESSOR MATCHES "aarch64")
            # arm euleros
            set(GE_SYS_ARCH "aarch64")
        else()
            message(FATAL_ERROR "Running on a unsupported architecture: ${SYSTEM_TYPE}, build terminated")
        endif()
        set(GE_LIB_PATH ${GE_LIB_PATH}/${GE_SYS_ARCH})
        set(STATIC_ACL_LIB ${GE_LIB_PATH})
        find_module(slog libslog.so ${GE_LIB_PATH})
        find_module(mmpa libmmpa.so ${GE_LIB_PATH})
        find_module(msprof libmsprof.so ${GE_LIB_PATH})
        find_module(hccl libhccl.so ${GE_LIB_PATH})
        find_module(adump_server libadump_server.a ${GE_LIB_PATH})
        find_module(runtime libruntime.so ${GE_LIB_PATH})
        find_module(runtime_compile libruntime_compile.so ${GE_LIB_PATH})
        find_module(resource libresource.so ${GE_LIB_PATH})
        find_module(error_manager liberror_manager.so ${GE_LIB_PATH})
        find_module(ascend_hal_stub libascend_hal.so ${GE_LIB_PATH})
        find_module(error_manager_static liberror_manager.a ${GE_LIB_PATH})
        find_module(msprofiler libmsprofiler.a ${GE_LIB_PATH})
    else()
        if(DEFINED ENV{ASCEND_CUSTOM_PATH})
            set(ASCEND_DIR $ENV{ASCEND_CUSTOM_PATH})
        else()
            set(ASCEND_DIR /usr/local/Ascend)
        endif()
        set(ASCEND_DRIVER_DIR ${ASCEND_DIR}/driver/lib64)
        set(ASCEND_DRIVER_COMMON_DIR ${ASCEND_DIR}/driver/lib64/common)
        set(ASCEND_DRIVER_SHARE_DIR ${ASCEND_DIR}/driver/lib64/share)
        set(ASCEND_RUNTIME_DIR ${ASCEND_DIR}/fwkacllib/lib64)
        set(ASCEND_ATC_DIR ${ASCEND_DIR}/atc/lib64)
        set(ASCEND_ACL_DIR ${ASCEND_DIR}/acllib/lib64)
        set(STATIC_ACL_LIB ${ASCEND_ACL_DIR})
        find_module(slog libslog.so ${ASCEND_ATC_DIR})
        find_module(mmpa libmmpa.so ${ASCEND_ATC_DIR})
        if(PLATFORM STREQUAL "train")
            find_module(msprof libmsprof.so ${ASCEND_DRIVER_COMMON_DIR})
            find_module(hccl libhccl.so ${ASCEND_RUNTIME_DIR})
            find_module(adump_server libadump_server.a ${ASCEND_RUNTIME_DIR})
            find_module(runtime libruntime.so ${ASCEND_RUNTIME_DIR})
            find_module(runtime_compile libruntime_compile.so ${ASCEND_ATC_DIR})
            find_module(resource libresource.so ${ASCEND_ATC_DIR})
            find_module(error_manager liberror_manager.so ${ASCEND_RUNTIME_DIR})
            find_module(msprofiler libmsprofiler.a ${ASCEND_RUNTIME_DIR})
            find_module(ascend_hal_stub libascend_hal.so ${ASCEND_DRIVER_DIR}/driver)
            if(PRODUCT STREQUAL "flr3")
                message(FATAL_ERROR "This platform is not supported in train mode, build terminated")
            endif()
        elseif(PLATFORM STREQUAL "inference")
            find_module(adump_server libadump_server.a ${ASCEND_ACL_DIR})
            find_module(runtime libruntime.so ${ASCEND_ACL_DIR})
            find_module(runtime_compile libruntime_compile.so ${ASCEND_ATC_DIR})
            find_module(resource libresource.so ${ASCEND_ATC_DIR})
            find_module(error_manager liberror_manager.so ${ASCEND_ATC_DIR})
            find_module(error_manager_static liberror_manager.a ${ASCEND_ACL_DIR})
            find_module(msprofiler libmsprofiler.a ${ASCEND_ACL_DIR})
            if(PRODUCT STREQUAL "flr3")
                find_module(msprof libmsprof.so ${ASCEND_DRIVER_SHARE_DIR})
            elseif(PRODUCT STREQUAL "flr1")
                find_module(ascend_hal_stub libascend_hal.so ${ASCEND_DRIVER_DIR}/driver)
                find_module(msprof libmsprof.so ${ASCEND_DRIVER_COMMON_DIR})
            elseif(PRODUCT STREQUAL "flr2")
                # flr2 ascend_hal_stub limsprof ?
            else()
                find_module(ascend_hal_stub libascend_hal.so ${ASCEND_DRIVER_DIR})
                find_module(msprof libmsprof.so ${ASCEND_DRIVER_DIR})
            endif()
        elseif(PLATFORM STREQUAL "all")
            find_module(msprof libmsprof.so ${ASCEND_DRIVER_COMMON_DIR})
            find_module(hccl libhccl.so ${ASCEND_RUNTIME_DIR})
            find_module(adump_server libadump_server.a ${ASCEND_RUNTIME_DIR})
            find_module(runtime_compile libruntime_compile.so ${ASCEND_ATC_DIR})
            find_module(runtime libruntime.so ${ASCEND_RUNTIME_DIR})
            find_module(resource libresource.so ${ASCEND_ATC_DIR})
            find_module(error_manager liberror_manager.so ${ASCEND_RUNTIME_DIR})
            find_module(error_manager_static liberror_manager.a ${ASCEND_ACL_DIR})
            find_module(msprofiler libmsprofiler.a ${ASCEND_RUNTIME_DIR})
            find_module(ascend_hal_stub libascend_hal.so ${ASCEND_DRIVER_DIR}/driver)
        else()
             message(FATAL_ERROR "PLATFORM param is invalid, should be train or inference, build terminated")
        endif()
    endif()

    set(METADEF_DIR ${CMAKE_CURRENT_LIST_DIR}/metadef)
    set(PARSER_DIR ${CMAKE_CURRENT_LIST_DIR}/parser)
    set(GE_DEPEND_DIR ${CMAKE_CURRENT_LIST_DIR}/..)

    add_subdirectory(metadef)
    add_subdirectory(parser)
    #add_subdirectory(metadef/graph)
    #add_subdirectory(metadef/register)
else()
    set(METADEF_DIR ${CMAKE_CURRENT_LIST_DIR}/../metadef)
    set(PARSER_DIR ${CMAKE_CURRENT_LIST_DIR}/../parser)
    set(GE_DEPEND_DIR ${CMAKE_CURRENT_LIST_DIR}/..)
endif()

add_subdirectory(ge)
